package java课题设计.功能类;
import java课题设计.工具类.Seek;
import java课题设计.工具类.Size;
 
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.io.*;
//此类用于读取学生数据并且显示表格
public class GenerateTable extends JFrame implements DocumentListener {
    //生成表格功能
    private JTable table;
    int NUM_OF_STUDENTS;
    private JFrame pre;
    int length= Seek.SEEK();
    public GenerateTable(JFrame pre) throws IOException {
        //调用方法来获取文本文件的长度
        Size b=new Size();
        NUM_OF_STUDENTS=b.getA();
        // 创建表格
        String[] columnNames = {"学生姓名","性别", "学号", "体育成绩", "Java成绩", "高数成绩", "出生年月日"};
        DefaultTableModel model = new DefaultTableModel(columnNames, NUM_OF_STUDENTS);
        table = new JTable(model);
 
 
 
        BufferedReader reader=new BufferedReader(new FileReader("D:\\\\\\\\aaa\\\\\\\\数据\\\\\\\\学生数据.txt"));
        String line = null;
        int i=0;
        while((line=reader.readLine())!=null){
            //将取出的数据进行分割
            String[] fields =line.split("\t");
            String name = fields[0];
            String sex = fields[1];
            String sid = fields[2];
            //double PEscore = Double.parseDouble(fields[3]);
            String PEscore = fields[3];
            //double Javascore = Double.parseDouble(fields[4]);
            String Javascore=fields[4];
            //double Mathscore = Double.parseDouble(fields[5]);
            String Mathscore=fields[5];
            String birthDate = fields[6];
 
 
            //将学生数据添加到表格中
            Object[] rowData = {name,sex, sid, PEscore,Javascore, Mathscore, birthDate};
            model.setValueAt(name, i, 0);
            model.setValueAt(sex,i,1);
            model.setValueAt(sid, i, 2);
            //model.setValueAt(String.format("%.0f", PEscore), i, 3);
            model.setValueAt(PEscore, i, 3);
            //model.setValueAt(String.format("%.0f", Javascore), i, 4);
            model.setValueAt(Javascore, i, 4);
            //model.setValueAt(String.format("%.0f", Mathscore), i, 5);
            model.setValueAt(Mathscore, i, 5);
            model.setValueAt(birthDate, i, 6);
            i++;
        }
        // 创建 JScrollPane
        JScrollPane scrollPane = new JScrollPane(table);
 
        // 创建 JFrame 并展示 UI 界面
        JFrame frame = new JFrame("学生信息表");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
 
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.add(scrollPane, BorderLayout.CENTER);
        frame.setContentPane(contentPane);
 
        frame.pack();
        frame.setVisible(true);
    }
    @Override
    protected void processWindowEvent(WindowEvent e){
        super.processWindowEvent(e);
        if(e.getID()==WindowEvent.WINDOW_CLOSED){
            this.setVisible(false);//隐藏当前窗口
            pre.setVisible(true);//显示上一级窗口
        }
    }
 
    @Override
    public void insertUpdate(DocumentEvent e) {
 
    }
 
    @Override
    public void removeUpdate(DocumentEvent e) {
 
    }
 
    @Override
    public void changedUpdate(DocumentEvent e) {
 
    }
}
